﻿
//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AnalysisServices;
namespace SMK_SSAS
{

    /// <summary>
    /// 
    /// </summary>
    public class AMO_Database_DataSource
    {
        /// <summary>
        /// 
        /// </summary>
        private Microsoft.AnalysisServices.DataSource amodatasource = null;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Microsoft.AnalysisServices.DataSource adodbdatasource()
        {
            if (amodatasource != null)
            {
                return amodatasource;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Datebase"></param>
        /// <param name="DataSourceID"></param>
        public void SetAMODatabaseDataSource(AMO_Database Datebase, string DataSourceID)
        {

            if (Datebase != null)
            {
                AMO_Server Server = Datebase.AMOServer();
                if (Server.Isconnected())
                {
                    if (Datebase.amodatabase().DataSources.Contains(DataSourceID))
                    {
                        amodatasource = Datebase.amodatabase().DataSources.Find(DataSourceID);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool HasDataSourcePermissions()
        {
            if (amodatasource != null)
            {
                if (amodatasource.DataSourcePermissions.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DataSources()
        {
            if (amodatasource != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.DataSourcePermission db in amodatasource.DataSourcePermissions)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodatasource.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
    }
}
